package cn.tedu.medicalpath.prescription.controller;

import cn.tedu.medicalpath.base.response.JsonResult;
import cn.tedu.medicalpath.prescription.pojo.dto.*;
import cn.tedu.medicalpath.prescription.pojo.vo.PrescriptionMedicineVO;
import cn.tedu.medicalpath.prescription.pojo.vo.PrescriptionVO;
import cn.tedu.medicalpath.prescription.service.PrescriptionMedicineService;
import cn.tedu.medicalpath.prescription.service.PrescriptionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * 处方单控制器
 * 涉及处方单的新增及修改
 * 涉及处方单药品的CRUD
 * 处方单药品与药品管理中的药品是不同的，因此新建了相关pojo
 */
@Slf4j
@RestController
@RequestMapping("/v1/prescription")
@Api(tags = "处方单模块")
public class PrescriptionController {
    @Autowired
    PrescriptionService prescriptionService;
    @Autowired
    PrescriptionMedicineService prescriptionMedicineService;

    /**
     * 保存处方单
     * 实现逻辑：根据前端提交的数据，保存到数据库中
     * @param prescriptionSaveParam
     * @return
     */
    @ApiOperation("保存处方单")
    @PostMapping("savePrescription")
    public JsonResult savePrescription(@RequestBody PrescriptionSaveParam prescriptionSaveParam){
        log.debug("Controller开始处理【保存处方单】的请求-入参：{}",prescriptionSaveParam);
        prescriptionService.savePrescription(prescriptionSaveParam);
        log.debug("Controller处理成功-【保存处方单】的请求-入参：{}",prescriptionSaveParam);
        return JsonResult.success();
    }

    /**
     * 新增处方单药品
     * 实现逻辑：根据处方单ID，关联到处方单药品表，新增一条处方单药品数据
     * 每新增一个药品就执行一次该方法
     * @param prescriptionMedicineSaveParam
     * @return
     */
    @ApiOperation("保存处方单药品")
    @PostMapping("savePrescriptionMedicine")
    public JsonResult savePrescriptionMedicine(PrescriptionMedicineSaveParam prescriptionMedicineSaveParam){
        log.debug("Controller开始处理【新增处方单药品】的请求-入参：{}",prescriptionMedicineSaveParam);
        prescriptionMedicineService.savePrescriptionMedicine(prescriptionMedicineSaveParam);
        log.debug("Controller处理成功-【新增处方单药品】的请求-入参：{}",prescriptionMedicineSaveParam);
        return JsonResult.success();
    }

    /**
     * 修改处方单药品
     */
    /*@ApiOperation("修改处方单药品")
    @PostMapping("updatePrescriptionMedicine")
    public JsonResult updatePrescriptionMedicine(PrescriptionMedicineSaveParam prescriptionMedicineSaveParam){
        log.debug("Controller开始处理-【修改处方单药品】的请求-入参：{}",prescriptionMedicineSaveParam);
        prescriptionMedicineService.updatePrescriptionMedicine(prescriptionMedicineSaveParam);
        return JsonResult.success();
    }*/

    /**
     * 删除处方单药品
     */
    @ApiOperation("删除处方单药品")
    @PostMapping("deletePrescriptionMedicine")
    public JsonResult deletePrescriptionMedicine(PrescriptionMedicineQuery prescriptionMedicineQuery){
        log.debug("Controller开始处理-【删除处方单药品】的请求-入参：{}",prescriptionMedicineQuery);
        prescriptionMedicineService.deletePrescriptionMedicine(prescriptionMedicineQuery);
        return JsonResult.success();
    }

    /**
     * 查询处方单
     * 实现逻辑：根据查询条件，关联到处方表，查询出所有的处方单数据
     * @param prescriptionQuery
     * @return
     */
    @ApiOperation("查询处方单")
    @GetMapping("selectPrescription")
    public JsonResult selectPrescription(PrescriptionQuery prescriptionQuery){
        log.debug("Controller开始处理-【查询处方单】的请求-入参：{}",prescriptionQuery);
        List<PrescriptionVO> list=prescriptionService.selectPrescription(prescriptionQuery);
        log.debug("Controller查询结果-【查询处方单】的请求-入参：{},出参：{}",prescriptionQuery,list);
        return JsonResult.success(list);

    }

    /*@ApiOperation("更新处方单的价格")
    @PostMapping("updatePrescriptionPrice/{id}")
    public JsonResult updatePrescriptionPriceByPrescriptionId(@PathVariable Long id){
        log.debug("Controller开始处理-【更新处方单价格】的请求-入参处方单id：{}",id);
        prescriptionService.updatePrescriptionPriceByPrescriptionId(id);
        return JsonResult.success();
    }*/

    /**
     * 查处方单药品
     * 实现逻辑：根据处方单ID，关联到处方单药品表，查询出所有的处方单药品数据
     * @param prescriptionMedicineQuery
     * @return
     */
    @ApiOperation("查询处方单药品")
    @GetMapping("selectPrescriptionMedicine")
    public JsonResult selectPrescriptionMedicine(PrescriptionMedicineQuery prescriptionMedicineQuery){
        log.debug("Controller开始处理-【查询处方单药品】的请求-入参：{}",prescriptionMedicineQuery);
        List<PrescriptionMedicineVO> list=prescriptionMedicineService.selectPrescriptionMedicine(prescriptionMedicineQuery);
        log.debug("Controller查询结果-【查询处方单药品】的请求-入参：{},出参：{}",prescriptionMedicineQuery,list);
        return JsonResult.success(list);

    }

    /**
     * 修改处方单状态、备注
     * @param prescriptionUpdateParam
     * @return
     */
    @ApiOperation("修改处方单状态、备注")
    @ApiModelProperty(value = "修改处方单状态、备注")
    @PostMapping("update")
    public JsonResult updatePrescriptionById(@RequestBody PrescriptionUpdateParam prescriptionUpdateParam){
        log.debug("Controller开始处理-【修改处方单状态、备注】的请求-入参：{}",prescriptionUpdateParam);
        prescriptionService.updatePrescriptionById(prescriptionUpdateParam);
        return JsonResult.success();
    }
}
